using UnityEngine;

public class GameTimeKeeper : MonoBehaviour
{
	private enum TimerState
	{
		reset = 0,
		running = 1,
		paused = 2,
		hitlimit = -1
	}

	private static GameTimeKeeper instance;

	private static GameObject parentObject;

	private float gameTimeLimit = 3599.99f;

	private TimerState sessionTimerState;

	private float sessionTimeKeep;

	private float sessionTimeStart;

	public static GameTimeKeeper GetInstance()
	{
		if (!instance)
		{
			parentObject = GameObject.Find("GameTimeKeeper");
			if (parentObject != null)
			{
				instance = parentObject.GetComponent<GameTimeKeeper>();
			}
			else
			{
				parentObject = new GameObject("GameTimeKeeper");
				instance = parentObject.AddComponent<GameTimeKeeper>();
			}
		}
		return instance;
	}

	public void ResetTimer()
	{
		sessionTimerState = TimerState.reset;
		sessionTimeKeep = 0f;
		sessionTimeStart = 0f;
	}

	public void StartTimer()
	{
		if (sessionTimerState == TimerState.reset || sessionTimerState == TimerState.paused)
		{
			sessionTimerState = TimerState.running;
			sessionTimeStart = Time.realtimeSinceStartup;
		}
	}

	public void StopTimer()
	{
		if (sessionTimerState == TimerState.running)
		{
			sessionTimerState = TimerState.paused;
			sessionTimeKeep += Time.realtimeSinceStartup - sessionTimeStart;
			if (sessionTimeKeep > gameTimeLimit)
			{
				sessionTimerState = TimerState.hitlimit;
				sessionTimeKeep = gameTimeLimit;
			}
		}
	}

	public bool IsStarted()
	{
		if (sessionTimerState == TimerState.reset)
		{
			return false;
		}
		return true;
	}

	public float GetTimer(int decimals = 2)
	{
		float num = sessionTimeKeep;
		if (sessionTimerState == TimerState.running)
		{
			num += Time.realtimeSinceStartup - sessionTimeStart;
		}
		if (num > gameTimeLimit)
		{
			sessionTimerState = TimerState.hitlimit;
			sessionTimeKeep = gameTimeLimit;
			num = gameTimeLimit;
		}
		return Utils.RoundFloat(num, decimals);
	}

	public void GetTimer(out int minutes, out int seconds, out int hundreds)
	{
		float timer = GetTimer();
		Utils.GetSplitedTime(timer, out minutes, out seconds, out hundreds);
	}

	public override string ToString()
	{
		int minutes;
		int seconds;
		int hundreds;
		GetTimer(out minutes, out seconds, out hundreds);
		return minutes.ToString("00") + ":" + seconds.ToString("00") + "." + hundreds.ToString("00");
	}
}
